/**
 *
 */
public class CountSort {
    public static void countSort(int[] array) {

        int max = array[0];
        int min = array[0];
        for (int i = 1; i < array.length; i++) {
            if(max < array[i]) {
                max = array[i];
            }
            if(min > array[i]) {
                min = array[i];
            }
        }
        //max  min
        int[] count = new int[max-min+1];

        //计数
        for (int i = 0; i < array.length; i++) {
            int index = array[i]-min;
            count[index] ++ ;
        }

        //覆盖原数组
        int index = 0;//表示array的下标
        //count数组 已经完成了计数
        for (int i = 0; i < count.length; i++) {
            while (count[i] != 0) {
                array[index] = i+min;//0+90
                index++;//2
                count[i]--;//0
            }
        }
    }

    public static void main(String[] args) {
        int[] array1={0,5,9,7,8,3,1,2,6,4};
        countSort(array1);
        for (int i :
                array1) {
            System.out.println(i);
        }
    }
}
